package com.xyz.androidiptest;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.RemoteCallbackList;
import android.os.RemoteException;
import android.os.SystemClock;
import android.util.Log;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicBoolean;

public class BookManagerService extends Service {

    private String TAG = "BMS";

    private AtomicBoolean isDestory = new AtomicBoolean(false);

    private CopyOnWriteArrayList<Book> mBookList = new CopyOnWriteArrayList<>();

    private RemoteCallbackList<IOnNewBookArrivedListenner> mListenners = new RemoteCallbackList<>();

    private Binder mBinder = new IBookManager.Stub() {
        //我们要在这里实现IBookManager.aidl中的方法；属于服务端的实现
        @Override
        public List<Book> getBookList() throws RemoteException {
            SystemClock.sleep(20000);
            return mBookList;
        }

        @Override
        public void addBook(Book book) throws RemoteException {
            mBookList.add(book);
        }

        @Override
        public void registerListenner(IOnNewBookArrivedListenner listenner) throws RemoteException {
            mListenners.register(listenner);
            Log.e("TAG", "register success, current size is:" + mListenners.getRegisteredCallbackCount());
        }

        @Override
        public void unregisterListenner(IOnNewBookArrivedListenner listenner) throws RemoteException {
            mListenners.unregister(listenner);
            Log.e("TAG", "unregister success, current size is :" + mListenners.getRegisteredCallbackCount());
        }
    };

    @Override
    public void onCreate() {
        super.onCreate();
        mBookList.add(new Book(1, "赵力"));
        mBookList.add(new Book(2, "杨盼"));
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (!isDestory.get()) {
                    try {
                        Thread.sleep(5000);
                        int bookId = mBookList.size() + 1;
                        Book newBook = new Book(bookId, "new book#" + bookId);
                        onNewBookArrives(newBook);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

    }

    private void onNewBookArrives(Book book) throws RemoteException {
        mBookList.add(book);
        final int n = mListenners.beginBroadcast();
        for (int i = 0; i < n; i++) {
            IOnNewBookArrivedListenner iOnNewBookArrivedListenner = mListenners.getBroadcastItem(i);
            iOnNewBookArrivedListenner.onNewBookArried(book);
        }
        mListenners.finishBroadcast();
    }

    public BookManagerService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    @Override
    public void onDestroy() {
        isDestory.set(true);
        super.onDestroy();
    }
}
